package com.shangqing.document_management.handler;


import com.shangqing.document_management.constant.Result;
import com.shangqing.document_management.entity.DocumentRegistration;
import com.shangqing.document_management.service.IDocumentRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2020-12-22
 */
@RestController
@Slf4j
@RequestMapping("/document")
public class DocumentRegistrationHandler {
    @Autowired
    IDocumentRegistrationService iDocumentRegistrationService;
    @PostMapping("/document_registration")
    public Result DocumentRegistration(@RequestBody @Valid DocumentRegistration documentRegistration, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            bindingResult.getFieldErrors().forEach(fieldError -> {
             log.info("发文管理新增校验失败:"+fieldError.getCode()+"====="+fieldError.getDefaultMessage());
            });

            return new Result(400,"新增校验失败");

        }
        Boolean flag=false;
        flag=iDocumentRegistrationService.addDocumentRegistration(documentRegistration);
        if(flag==true){
            log.info("发文管理新增成功");
            return new Result(200,"新增成功","document_registration",documentRegistration);
        }
        log.info("发文管理新增失败");
        return new Result(400,"新增失败");
    }

}
